package abq2.jic.jic.plugin.operate.course;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.MobileFormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.BillList;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.report.ReportList;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.DispatchServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;

/**
 * 单据界面插件
 */
public class QChooseCourse extends AbstractBillPlugIn implements Plugin {
    String act_id = "chooseConf";
    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {

        super.beforeDoOperation(e);
        String operateKey = ((FormOperate) e.getSource()).getOperateKey();
        ReportList reportList = this.getControl("reportlistap");
        if(((FormOperate)e.getSource()).getOperateKey().equals("choosemycourse")){

            Object primaryKeyValue = null;


            int[] selectedRows = reportList.getEntryState().getSelectedRows();
            for (int selectedRow : selectedRows) {
                DynamicObject rowData = reportList.getReportModel().getRowData(selectedRow);
                String abq2_number = rowData.getString("abq2_number");
                DynamicObject dy = BusinessDataServiceHelper.loadSingle("abq2_stu_course",
                        "number,id",
                        (new QFilter("number", QCP.equals, abq2_number)).toArray());
                primaryKeyValue = dy.getLong("id");
                break;
            }

            //弹窗界面
            FormShowParameter formShowParameter = new MobileFormShowParameter();
            formShowParameter.setFormId("abq2_course_window");
            formShowParameter.setCustomParam("courseId",primaryKeyValue);
            formShowParameter.setCloseCallBack(new CloseCallBack(this,act_id));
            formShowParameter.getOpenStyle().setShowType(ShowType.Modal);
            this.getView().showForm(formShowParameter);
        }else if (operateKey.equals("giveupmycourse")){

            Object primaryKeyValue = null;


            int[] selectedRows = reportList.getEntryState().getSelectedRows();
            for (int selectedRow : selectedRows) {
                DynamicObject rowData = reportList.getReportModel().getRowData(selectedRow);
                String abq2_number = rowData.getString("abq2_number");
                DynamicObject dy = BusinessDataServiceHelper.loadSingle("abq2_stu_course",
                        "number,id",
                        (new QFilter("number", QCP.equals, abq2_number)).toArray());
                primaryKeyValue = dy.getLong("id");
                break;
            }

            DynamicObject[] myCourses = BusinessDataServiceHelper.load("abq2_my_course",
                    "abq2_my_course_stu," + //课程内码
                            "name," + //课程名称
                            "abq2_textfield3," + //星期
                            "abq2_textfield1," + //教室
                            "abq2_textfield," + //教师
                            "abq2_textfield4," + //年级
                            "creator," + //学生
                            "abq2_textfield2",  //课节
                    new QFilter[]{new QFilter("creator", QCP.equals, RequestContext.get().getCurrUserId())});
            boolean resultHas = true;
            for (DynamicObject myCours : myCourses) {
                String courseId = myCours.getString("abq2_my_course_stu.id");
                if (courseId.equals(primaryKeyValue.toString())){
                    resultHas = false;
                    break;
                }
            }
            if (resultHas){
                this.getView().showTipNotification("你未选择该课程，无需退选");
                return;
            }

            DynamicObject optionCourse = BusinessDataServiceHelper.loadSingle("abq2_stu_course",
                    "id," + //内码
                            "abq2_combofield1," + //星期
                            "abq2_classroom," + //教室
                            "abq2_textfield1," + //教师
                            "abq2_combofield," + //年级
                            "abq2_integerfield," + //课程人数
                            "abq2_integerfield1," + //已选人数
                            "abq2_mulcombofield",  //课节
                    new QFilter[]{new QFilter("id",QCP.equals,primaryKeyValue)});
            int courseInt = optionCourse.getInt("abq2_integerfield1");
            optionCourse.set("abq2_integerfield1", courseInt - 1);
            SaveServiceHelper.update(optionCourse);


            DeleteServiceHelper.delete("abq2_my_course",new QFilter[]{new QFilter("abq2_my_course_stu", QCP.equals,primaryKeyValue)});

            ReportList reportList1 = this.getControl("reportlistap");
            reportList1.refresh();

            this.getView().showSuccessNotification("退选成功");
        }else if (operateKey.equals("showai")){
            String pageId = this.getView().getMainView().getPageId();
            Object pkValue = Long.parseLong("1966190500306238464");
            DispatchServiceHelper.invokeBizService("ai", "gai", "GaiService","selectProcessInSideBar",pkValue,pageId,"此技能用于智能自动选课");
        }
    }

    @Override
    public void closedCallBack(ClosedCallBackEvent a) {
        super.closedCallBack(a);
        String actionId = a.getActionId();
        if (actionId.equalsIgnoreCase(act_id)){
            Object returnData = a.getReturnData();
            if (returnData != null){
                Boolean b = (Boolean) returnData;
                if (b){
                    this.getView().showSuccessNotification("选课成功");
                }
            }
        }
        ReportList reportList1 = this.getControl("reportlistap");
        reportList1.refresh();
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs afterDoOperationEventArgs) {
        super.afterDoOperation(afterDoOperationEventArgs);
    }
}